<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主机名称" prop="hostName">
        <el-input
          v-model="queryParams.hostName"
          placeholder="请输入主机名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="系统版本" prop="systemVersion">
        <el-input
          v-model="queryParams.systemVersion"
          placeholder="请输入系统版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="程序名称" prop="programName">
        <el-input
          v-model="queryParams.programName"
          placeholder="请输入程序名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件版本" prop="programFileVersion">
        <el-input
          v-model="queryParams.programFileVersion"
          placeholder="请输入程序文件版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品版本" prop="programProductVersion">
        <el-input
          v-model="queryParams.programProductVersion"
          placeholder="请输入程序产品版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['acquisition:program:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['acquisition:program:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['acquisition:program:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['acquisition:program:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="programList" stripe border highlight-selection-row
              @selection-change="handleSelectionChange" @row-dblclick="recordDetail">
      <el-table-column type="selection" width="40" align="center"/>
      <el-table-column label="ID" width="50" align="center" prop="id"/>
      <el-table-column label="系统版本" width="180" align="center" prop="systemVersion" show-overflow-tooltip/>
      <el-table-column label="主机名称" min-width="220" align="center" prop="hostName" show-overflow-tooltip/>
      <el-table-column label="程序名称" min-width="250" align="center" prop="programName" show-overflow-tooltip/>
      <el-table-column label="程序文件版本" width="170" align="center" prop="programFileVersion" show-overflow-tooltip/>
      <el-table-column label="程序产品版本" width="170" align="center" prop="programProductVersion" show-overflow-tooltip/>
      <el-table-column label="程序路径" min-width="220" align="center" prop="programPath" show-overflow-tooltip/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="190" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="recordDetail(scope.row)"
            v-hasPermi="['acquisition:program:record']"
          >使用记录
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['acquisition:program:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['acquisition:program:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改主机程序对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="主机名称" prop="hostName">
          <el-input v-model="form.hostName" placeholder="请输入主机名称"/>
        </el-form-item>
        <el-form-item label="系统版本" prop="systemVersion">
          <el-input v-model="form.systemVersion" placeholder="请输入系统版本"/>
        </el-form-item>
        <el-form-item label="程序名称" prop="programName">
          <el-input v-model="form.programName" placeholder="请输入程序名称"/>
        </el-form-item>
        <el-form-item label="程序文件版本" prop="programFileVersion">
          <el-input v-model="form.programFileVersion" placeholder="请输入程序文件版本"/>
        </el-form-item>
        <el-form-item label="程序产品版本" prop="programProductVersion">
          <el-input v-model="form.programProductVersion" placeholder="请输入程序产品版本"/>
        </el-form-item>
        <el-form-item label="程序路径" prop="programPath">
          <el-input v-model="form.programPath" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看程序使用记录对话框 -->
    <el-dialog :title="title" :visible.sync="openRecord" width="900px" append-to-body>
      <el-descriptions title="程序信息" :column="3">
        <el-descriptions-item label="主机名称">{{ form.hostName }}</el-descriptions-item>
        <el-descriptions-item label="程序名称">{{ form.programName }}</el-descriptions-item>
        <el-descriptions-item label="程序文件版本">{{ form.programFileVersion }}</el-descriptions-item>
        <el-descriptions-item label="程序产品版本">{{ form.programProductVersion }}</el-descriptions-item>
        <el-descriptions-item label="系统版本">{{ form.programFileVersion }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ form.createTime }}</el-descriptions-item>
        <el-descriptions-item label="程序路径">{{ form.programPath }}</el-descriptions-item>
      </el-descriptions>
      <el-collapse  v-model="accordion">
        <el-collapse-item name="useRecord">
          <template slot="title">
            <i class="el-icon-s-grid"></i> 使用记录详情
          </template>
          <div>
            <el-table v-loading="loading" :data="recordList" stripe border highlight-selection-row>
              <el-table-column label="ID" width="50" align="center" prop="id"/>
              <el-table-column label="进程PID" width="80" align="center" prop="processPid"/>
              <el-table-column label="程序启动时间" align="center" prop="programStartTime" width="160">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.programStartTime) }}</span>
                </template>
              </el-table-column>
              <el-table-column label="最后运行时间" align="center" prop="lastRunTime" width="160">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.lastRunTime) }}</span>
                </template>
              </el-table-column>
              <el-table-column label="登录账号" align="center" prop="loginAccount"/>
              <el-table-column label="登录时间" align="center" prop="loginTime" width="160">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.loginTime) }}</span>
                </template>
              </el-table-column>
            </el-table>
            <pagination
              v-show="totalRecord>0"
              :total="totalRecord"
              :page.sync="queryParamsRecord.pageNum"
              :limit.sync="queryParamsRecord.pageSize"
              @pagination="getListRecord(form.id)"
            />
          </div>
        </el-collapse-item>
        <el-collapse-item name="networkCard">
          <template slot="title">
            <i class="el-icon-tickets"></i> 网卡信息
          </template>
          <div>
            <el-table v-loading="loading" :data="infoList" stripe border highlight-selection-row>
              <el-table-column label="ID" width="50" align="center" prop="id"/>
              <el-table-column label="MAC地址" width="220" align="center" prop="macAddress"/>
              <el-table-column label="IP地址" align="center" prop="ipAddress"/>
                               show-overflow-tooltip/>
              <el-table-column label="创建时间" align="center" prop="createTime" width="160">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
              </el-table-column>
            </el-table>

            <pagination
              v-show="totalInfo>0"
              :total="totalInfo"
              :page.sync="queryParamsInfo.pageNum"
              :limit.sync="queryParamsInfo.pageSize"
              @pagination="getListInfo(form.id)"
            />
          </div>
        </el-collapse-item>
      </el-collapse>
      <div slot="footer" class="dialog-footer">
        <el-button @click="()=>openRecord = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listProgram, getProgram, delProgram, addProgram, updateProgram} from "@/api/acquisition/program";
import {listInfo} from "@/api/acquisition/info";
import {listRecord} from "@/api/acquisition/record";

export default {
  name: "Program",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 总条数网卡
      totalInfo: 0,
      // 总条数使用记录
      totalRecord: 0,
      // 主机程序表格数据
      programList: [],
      // 网卡表格数据
      infoList: [],
      // 使用记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 使用记录
      openRecord: false,
      accordion: ['useRecord', 'networkCard'],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hostName: null,
        systemVersion: null,
        programPath: null,
        programName: null,
        programFileVersion: null,
        programProductVersion: null,
      },
      // 查询参数
      queryParamsInfo: {
        pageNum: 1,
        pageSize: 10,
        hostProgramId: null
      },
      // 查询参数
      queryParamsRecord: {
        pageNum: 1,
        pageSize: 10,
        hostProgramId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hostName: [
          {required: true, message: "主机名称不能为空", trigger: "blur"}
        ],
        systemVersion: [
          {required: true, message: "系统版本不能为空", trigger: "blur"}
        ],
        programPath: [
          {required: true, message: "程序路径不能为空", trigger: "blur"}
        ],
        programName: [
          {required: true, message: "程序名称不能为空", trigger: "blur"}
        ],
        programFileVersion: [
          {required: true, message: "程序文件版本不能为空", trigger: "blur"}
        ],
        programProductVersion: [
          {required: true, message: "程序产品版本不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询主机程序列表 */
    getList() {
      this.loading = true;
      listProgram(this.queryParams).then(response => {
        this.programList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        hostName: null,
        systemVersion: null,
        programPath: null,
        programName: null,
        programFileVersion: null,
        programProductVersion: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加主机程序";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProgram(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改主机程序";
      });
    },
    recordDetail(row) {
      const id = row.id
      this.queryParamsInfo.hostProgramId = id;
      listInfo(this.queryParamsInfo).then(response => {
        this.infoList = response.rows;
        this.totalInfo = response.total;
        this.loading = false;
      });
      this.queryParamsRecord.hostProgramId = id;
      listRecord(this.queryParamsRecord).then(response => {
        this.recordList = response.rows;
        this.totalRecord = response.total;
        this.loading = false;
      });
      getProgram(id).then(response => {
        this.form = response.data;
        this.openRecord = true;
        this.title = "软件使用记录";
      });
    },
    getListInfo(id) {
      this.queryParamsInfo.hostProgramId = id;
      listInfo(this.queryParamsInfo).then(response => {
        this.infoList = response.rows;
        this.totalInfo = response.total;
        this.loading = false;
      });
    },
    getListRecord(id) {
      this.queryParamsRecord.hostProgramId = id;
      listRecord(this.queryParamsRecord).then(response => {
        this.recordList = response.rows;
        this.totalRecord = response.total;
        this.loading = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProgram(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProgram(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除主机程序编号为"' + ids + '"的数据项？').then(function () {
        return delProgram(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('acquisition/program/export', {
        ...this.queryParams
      }, `program_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
